package com.thinkgem.jeesite.modules.qy.api;

import com.thinkgem.jeesite.common.utils.EhCacheUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.qy.entity.QyUser;
import com.thinkgem.jeesite.modules.qy.service.QyUserService;
import com.thinkgem.jeesite.modules.qy.utils.VuthCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by JohnShen on 2016/12/1.
 * 平台用户工具
 */
@RestController
@RequestMapping(value = "front/user")
public class QyUserApi  extends BaseController{

        protected Logger logger = LoggerFactory.getLogger(getClass());
        @Autowired
        private QyUserService qyUserService;

    /**
     * 根据登录名查找用户
     * @param loginName
     * @return
     */
        @RequestMapping(value="getUser")
        @ResponseBody
        public LinkedHashMap<String, Object> getUser(@RequestParam String loginName){
            LinkedHashMap<String,Object> map = new LinkedHashMap<String,Object>();
            QyUser user =  new QyUser();
            user.setLoginName(loginName);
            map.put("UserInfoBean" ,qyUserService.getUserByLoginName(user));
            return map;
        }

        /**
         *  用户注册
         * @param user
         * @param br
         * @param request
         * @param response
         * @return
         */
        @RequestMapping(value = "register")
        public LinkedHashMap<String, Object> insertUser(@Valid QyUser user , BindingResult br,
                                                        HttpServletRequest request, HttpServletResponse response){
            LinkedHashMap<String,Object> map = new LinkedHashMap<String,Object>();
            map.put("code", false);
            List<FieldError> errors = br.getFieldErrors();
            String code = request.getParameter("code");
            //获取session
            HttpSession  session =request.getSession();
            //服务器保存的验证码code
            //登录名
            String loginName = user.getEmail()!=null ? user.getEmail() : user.getMobile();
            String cacheCode =EhCacheUtils.get("VuthCode" , loginName).toString();
            if(!cacheCode.equals(code) ){
                map.put("errMessage","验证码错误");
                return map;
            }
            if(errors != null && !errors.isEmpty()){
                map.put("errMessage", errors.get(0).getDefaultMessage());
            }else{
                    user.setLoginName(loginName);
                    logger.info("register loginName : "+loginName);
                if(qyUserService.getUserByLoginName(user) != null  ){ //验证用户是否已经存在
                    map.put("errMessage","改用户已注册");
                }else{
                    qyUserService.save(user);
                    map.put("code",true);
                }
            }
            return map;
        }

    //发送验证码
    @RequestMapping(value = "verifyCode")
    @ResponseBody
    public LinkedHashMap<String, Object> sendCode(@RequestParam (required = true ) String recipient,
                          @RequestParam (required = true ) String type, HttpServletRequest request){
        LinkedHashMap<String,Object> map = new LinkedHashMap<String,Object>();
        map.put("code", false);
        //收件人信息
        Integer sendType = Integer.valueOf(type);
        Map<String,Object>  messageMap = new HashMap<String ,Object>();
        messageMap.put(VuthCode.TYPE , sendType);
        messageMap.put(VuthCode.RECIPIENT , recipient);
        String result = VuthCode.sendCode(messageMap ,request.getSession()); //发送邮件
        map.put("result",result);
        return map;
    }
}
